//
//  CaremeRecommendViewController.m
//  UI_xiangmu
//
//  Created by dllo on 15/4/24.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "CaremeRecommendViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "RecommandCaremeCellTableViewCell.h"
#import "ListSingerViewController.h"
#import "ClassifyCaremeViewController.h"
#import "ListDownModel.h"
#import "ListUpModel.h"
#import "ListUpCell.h"
#import "ClassifyModel.h"
#import "ClassifyCaremeCell.h"
#import "ClassifyCollectionReusableView.h"
#import "ClassifyOneViewController.h"
#import "detailCamereViewController.h"
#import "ListDownViewController.h"
#import "searchCameraViewController.h"
#import "MJRefresh.h"
@interface CaremeRecommendViewController ()
@property(nonatomic,retain)UISegmentedControl *SegCamere;

@property(nonatomic,retain)NSMutableArray *modelarr;

@property(nonatomic,retain)NSMutableArray *ListupArr;

@property(nonatomic,retain)NSMutableArray *ListdownArr;

@property(nonatomic,retain)NSMutableArray *ClassifyArr;

@property(nonatomic,retain)NSMutableArray *ClassifyArr2;
@property(nonatomic,retain)NSMutableArray *ClassifyArr3;

@property(nonatomic,retain)UITableView *tableview2;

@property(nonatomic,retain)UIScrollView *scro;

@property(nonatomic,retain)UICollectionView *classifyCollectionView;
@property(nonatomic, assign)NSInteger page;

@property(nonatomic,copy)NSString *lastdd;

@end

@implementation CaremeRecommendViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        self.ListdownArr = [NSMutableArray array];
        self.ListupArr = [NSMutableArray array];
        self.modelarr = [NSMutableArray array];
        self.ClassifyArr = [NSMutableArray array];
        self.ClassifyArr2 = [NSMutableArray array];
        self.ClassifyArr3 = [NSMutableArray array];
        self.page = 1;
        self.lastdd = @"0";
        
    }
    [self data];
    [self data2];
    [self data3];
   
    return self;
}
#pragma mark 数据解析
-(void)data
{
  AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Cartoon&ios=23&pad=0&a=newIndex&tab=1&page=%ld&limit=15&hasTopic=1", self.page];
    ;
    
    [manager GET:str parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"list"];
        
        for (NSMutableDictionary *dic in arr) {
            RecommendCamereModel *model = [[RecommendCamereModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.modelarr addObject:model];
            [model release];
        }
        
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

-(void)data2
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=List&ios=23&pad=0&last_id=%@",self.lastdd];
    [manager GET:str parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr1 = [dic objectForKey:@"rank"];
        NSMutableArray *arr2 = [dic objectForKey:@"topic"];
        for (NSMutableDictionary *dic in arr1) {
            ListUpModel *model = [[ListUpModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.ListupArr addObject:model];
            [model release];
            }
       for (NSMutableDictionary *dic in arr2) {
            ListDownModel *model = [[ListDownModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.ListdownArr addObject:model];
            [model release];
        }
        self.lastdd = [responseObject objectForKey:@"last_id"];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"解析失败");
    }];
}
-(void)data3
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://manhua.haomee.cn/?m=Cartoon&ios=23&pad=0&a=channelgroupNew" parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *arr = responseObject;
       NSMutableDictionary *dic = arr[0];
        NSMutableArray *arr1 = [dic objectForKey:@"list"];
        for (int i = 0; i<arr1.count; i++) {
        
        ClassifyModel *model = [[ClassifyModel alloc] init];
        [model setValuesForKeysWithDictionary:arr1[i]];
        [self.ClassifyArr addObject:model];
        [model release];
        }
        
        NSMutableDictionary *dic2 = arr[1];
        NSMutableArray *arr2 = [dic2 objectForKey:@"list"];
        for (NSMutableDictionary *dic in arr2) {
            ClassifyModel *model = [[ClassifyModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.ClassifyArr2 addObject:model];
            [model release];
        }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"解析失败");
    }];
}
#pragma mark 加载页面
- (void)viewDidLoad {
    [super viewDidLoad];
   
    // Do any additional setup after loading the view.
    self.SegCamere = [[UISegmentedControl alloc] initWithItems:@[@"动画推荐",@"动画片单",@"动画分类"]];
    self.navigationItem.titleView = self.SegCamere;
    [self.SegCamere addTarget:self action:@selector(segClick:) forControlEvents:UIControlEventValueChanged];
    self.SegCamere.selectedSegmentIndex = 0;
    [_SegCamere release];
    self.SegCamere.tintColor = [UIColor orangeColor];
     self.scro = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.scro.bounces = NO;
    self.scro.contentSize = CGSizeMake(self.view.frame.size.width * 3, self.scro.frame.size.height-64-49);
    [self.view addSubview:self.scro];
    [self.scro release];
    self.scro.delegate = self;
    [self select];
    [self select2];
    [self select3];
    self.scro.pagingEnabled = YES;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search.png"] style:UIBarButtonItemStylePlain target:self action:@selector(searchclick:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor orangeColor];
    [self method];
    [self method1];
}
#pragma mark 拼接页面
-(void)select{
    self.view.backgroundColor = [UIColor cyanColor];
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.scro.frame.size.height-64-49) style:UITableViewStylePlain];
    [self.scro addSubview:self.myTableView];
    [self.myTableView release];
    self.myTableView.dataSource = self;
    self.myTableView.delegate  =self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   }

-(void)select2
{
    UICollectionViewFlowLayout *flowlayout=[[UICollectionViewFlowLayout alloc]init];
    flowlayout.itemSize=CGSizeMake(170 , 150);
    //最小列间距
    flowlayout.minimumInteritemSpacing=10;
    //最小行间距
    flowlayout.minimumLineSpacing=5;
    flowlayout.scrollDirection=UICollectionViewScrollDirectionVertical;
    flowlayout.sectionInset=UIEdgeInsetsMake(10, 10, 15,15);
    self.myCollectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(375, 0, self.view.frame.size.width, 310)  collectionViewLayout:flowlayout] ;
    self.myCollectionView.backgroundColor=[UIColor whiteColor];
    self.myCollectionView.dataSource=self;
    self.myCollectionView.delegate=self;
   self.myCollectionView.backgroundColor = [UIColor yellowColor];
    //注册cell
    [self.myCollectionView registerClass:[ListUpCell class] forCellWithReuseIdentifier:@"reuse1"];
    flowlayout.headerReferenceSize = CGSizeMake(375,100);
    
    
    self.tableview2 = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.scro.frame.size.width, self.scro.frame.size.height-64-49) style:UITableViewStylePlain];
    self.tableview2.delegate = self;
    self.tableview2.dataSource = self;
    [self.scro addSubview:self.tableview2];
    self.tableview2.tableHeaderView = self.myCollectionView;
    [_tableview2 release];
}
//第三个选项
-(void)select3
{
    UICollectionViewFlowLayout *flowlayout=[[UICollectionViewFlowLayout alloc]init];
    flowlayout.itemSize=CGSizeMake(100 , 80);
    //最小列间距
    flowlayout.minimumInteritemSpacing=10;
    //最小行间距
    flowlayout.minimumLineSpacing=1;
    flowlayout.scrollDirection=UICollectionViewScrollDirectionVertical;
    flowlayout.sectionInset=UIEdgeInsetsMake(7.5, 7.5, 5, 5);
    self.classifyCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(750, 0, self.view.frame.size.width, self.view.frame.size.height-64-49) collectionViewLayout:flowlayout];
    self.classifyCollectionView.delegate = self;
    self.classifyCollectionView.dataSource = self;
    [self.scro addSubview:self.classifyCollectionView];
    [_classifyCollectionView release];
    self.classifyCollectionView.backgroundColor = [UIColor greenColor];
    
    
    
    //注册页眉
    [self.classifyCollectionView registerClass:[ClassifyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    
        [self.classifyCollectionView registerClass:[ClassifyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot"];
    
    
    [self.classifyCollectionView registerClass:[ClassifyCaremeCell class] forCellWithReuseIdentifier:@"reuse2"];
    
}
#pragma mark 设置小标题
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{   if(collectionView == self.classifyCollectionView){
   
    CGSize size = {240,25};
    return size;
}
else{
    CGSize size = {0,0};
    return size;
}
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{   if(collectionView == self.classifyCollectionView){
    ClassifyCollectionReusableView *headview;
    if ([kind isEqual:UICollectionElementKindSectionHeader]) {
        headview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
        headview.backgroundColor = [UIColor redColor];
        if (indexPath.section == 0) {
            [headview setLabelText:@"推荐"];
            
        }else{
            [headview setLabelText:@"分类"];

        }

    }
    return headview;}
else{
    return nil;
}
}

#pragma mark 铺页面

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{if(collectionView == self.myCollectionView)
{
    return 1;
}else{
    return 2;
}
}

- (void)method1
{
    [self.tableview2 addLegendHeaderWithRefreshingBlock:^{
        [self.ListdownArr removeAllObjects];
        self.lastdd = 0;
        [self data2];
        [self.tableview2 reloadData];
        [self.tableview2.header endRefreshing];
    }];
    [self.tableview2.legendHeader beginRefreshing];
    [self.tableview2 addLegendFooterWithRefreshingBlock:^{
        [self data2];
        [self.tableview2 reloadData];
        [self.tableview2.footer endRefreshing];
    }];
}

- (void)method
{
        [self.myTableView addLegendHeaderWithRefreshingBlock:^{
        [self.modelarr removeAllObjects];
        self.page = 1;
        [self data];
        [self.myTableView reloadData];
        [self.myTableView.header endRefreshing];
    }];
    [self.myTableView.legendHeader beginRefreshing];
    [self.myTableView addLegendFooterWithRefreshingBlock:^{
        self.page++;
        [self data];
        [self.myTableView reloadData];
        [self.myTableView.footer endRefreshing];
    }];
}



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == self.myCollectionView) {
        return self.ListupArr.count;
    }else {
    if (section == 0) {
        return self.ClassifyArr.count;
    }
    else {
        return self.ClassifyArr2.count;
    }
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{if(collectionView == self.myCollectionView)
{ListUpCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse1" forIndexPath:indexPath];
    NSString *str = [self.ListupArr[indexPath.row] cover];
    NSString *strnew = [NSString stringWithFormat:@"%@_200_240.jpg",str];
    NSURL *url = [NSURL URLWithString:strnew];
    [cell.picimage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"48.jpg"]];
    cell.mylabel.text = [self.ListupArr[indexPath.row] title];
        cell.myimage.image = [UIImage imageNamed:@"6.png"];
    
    return cell;
}
else{
    ClassifyCaremeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse2" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        
        NSString *str = [NSString stringWithFormat:@"%@_200_240.jpg",[self.ClassifyArr[indexPath.row] pic]];
        NSURL *url = [NSURL URLWithString:str];
        [cell.myimage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"48.jpg"]];
        cell.mylabel.text = [self.ClassifyArr[indexPath.row] name];
        return cell;
    }else {
        NSString *str = [NSString stringWithFormat:@"%@_200_240.jpg",[self.ClassifyArr2[indexPath.row] pic]];
        NSURL *url = [NSURL URLWithString:str];
        [cell.myimage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"48.jpg"]];
        cell.mylabel.text = [self.ClassifyArr2[indexPath.row] name];
        return cell;
}
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{if(tableView == self.myTableView)
{
    return self.modelarr.count;}
else{
    return self.ListdownArr.count;
}
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    RecommandCaremeCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[RecommandCaremeCellTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    if (tableView == self.myTableView) {
        NSURL *url = [NSURL URLWithString:[self.modelarr[indexPath.row] pic]];
        cell.mylabel.text = [self.modelarr[indexPath.row] name];
        
        cell.footlabel.text = [self.modelarr[indexPath.row]lastup];
        [cell.myimage sd_setImageWithURL:url];
    }else{
        NSString *str = [NSString stringWithFormat:@"%@_200_240.jpg",[self.ListdownArr[indexPath.row] pic]];
        NSURL *url = [NSURL URLWithString:str];
        [cell.myimage sd_setImageWithURL:url];
        cell.mylabel.text = [self.ListdownArr[indexPath.row] intro];
    }
    
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200.0;
}

//实现segment
-(void)segClick:(UISegmentedControl *)seg
{
    if (seg.selectedSegmentIndex == 1) {
        self.scro.contentOffset = CGPointMake(375, -64);
        
    }else if(seg.selectedSegmentIndex == 2){
        self.scro.contentOffset = CGPointMake(750, -64);
    }else{
        self.scro.contentOffset = CGPointMake(0, -64);
}
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger i = self.scro.contentOffset.x/375;
    if (i == 0) {
        self.SegCamere.selectedSegmentIndex = 0;
    }else if(i == 1){
        self.SegCamere.selectedSegmentIndex = 1;
    }else{
        self.SegCamere.selectedSegmentIndex = 2;
    }
    
}

#pragma mark 实现页面推送

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.myCollectionView) {
        ListSingerViewController *singer = [[ListSingerViewController alloc] init];
        
        singer.canshu = [self.ListupArr[indexPath.row] id];
        [self.navigationController pushViewController:singer animated:YES];
        [singer release];
    }else{
        ClassifyCaremeViewController *class = [[ClassifyCaremeViewController alloc] init];
        if (indexPath.section == 0 && indexPath.row != 0) {
            class.canshu = [self.ClassifyArr[indexPath.row] id];
            [self.navigationController pushViewController:class animated:YES];
            [class release];
        }else if(indexPath.section == 1){
            class.canshu = [self.ClassifyArr2[indexPath.row] id];
            [self.navigationController pushViewController:class animated:YES];
            [class release];
        }else{
            ClassifyOneViewController *one = [[ClassifyOneViewController alloc] init];
            [self.navigationController pushViewController:one animated:YES];
            [one release];
            [class release];
            
        }

    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    detailCamereViewController *detile = [[detailCamereViewController alloc] init];
    if (tableView == self.myTableView) {
        detile.canshu = [self.modelarr[indexPath.row] id];
        detile.canshu = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Api&ios=23&pad=0&a=getInfo&video_id=%@&uid=(null)",detile.canshu];
        [self.navigationController pushViewController:detile animated:YES];
        [detile release];
}else{
    ListDownViewController *list = [[ListDownViewController alloc] init];
    list.canshu = [self.ListdownArr[indexPath.row] id];
    [self.navigationController pushViewController:list animated:YES];
    [list release];
}
}

-(void)searchclick:(UIBarButtonItem *)bu
{
    searchCameraViewController *search = [[searchCameraViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
    [search release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
